<template>
  <div>
    <!-- <attr-select :dataSource="dataSource" :filter="filter" v-model="key" /> -->
    <!-- <p>所选病例数：{{all_count}}；发病病例数{{valiad_count}}</p> -->
    <!-- <p> 高亮显示RR值大于<el-input-number size="mini" v-model="high_light" :min="0" :max="1" :precision="2" :step="0.1"/> </p> -->
     <div class="ana-detail-title" style="position: relative">
      <a-icon type="forward" />四表格法分析
    </div>
    <el-table :data="tableData" border :row-class-name="tableRowClassName">
      <el-table-column prop="x" label="蛋糕" width="140"> </el-table-column>
      <el-table-column prop="count1" label="病例组" width="140">
      </el-table-column>
      <el-table-column prop="count2" label="对照组" width="140">
      </el-table-column>
        <el-table-column prop="count" label="合计" width="140">
      </el-table-column>
      <!-- </el-table-column>
      <el-table-column label="病例组">
        
      </el-table-column>
      <el-table-column label="未暴露组">
        <el-table-column prop="count2" label="发病" width="140">
        </el-table-column>
        <el-table-column prop="ratio2" label="未发病" width="140">
        </el-table-column>
      </el-table-column>
      <el-table-column label="RR值">
        <el-table-column prop="RR" label="RR值" width="140"> </el-table-column>
        <el-table-column prop="RR_P" label="P值" width="140"> </el-table-column>
      </el-table-column> -->
    </el-table>
    <div class="info">
      OR=80×60/20×40=6.0，95%CI=3.1-12
    </div>
  </div>
</template>

<script>
import { isArray, isString } from "@/utils/utils";
import AttrSelect from "../utils/AttrSelect.vue";
import Moment from "@/utils/moment.zh_cn";
export default {
  components: {
    AttrSelect,
  },
  data() {
    return {
      key: "食物",
      tableData: [],
      all_count: 0,
      valiad_count: 0,
      high_light: 0.8,
    };
  },
  props: {
    dataSource: Array,
  },
  watch: {
    key: function (v, o) {
      this.reproject();
    },
    dataSource: function (v, o) {
      this.reproject();
    },
    high_light: function (v, o) {
      // this.reproject();
    },
  },
  methods: {
    tableRowClassName({ row, rowIndex }) {
      if (row["RR"] >= this.high_light) {
        return "warning-row";
      }
      return "";
    },

    reproject() {
      let stateKey = "发病人员";
      let data = this.dataSource.reduce((pre, value) => {
        if (value[stateKey]) {
          let sets = pre["k1"] || {};
          if (value[this.key]) {
            let v = value[this.key];
            if (isArray(v) && v.length && v.length > 0) {
              v.forEach((vv) => {
                if (isString(vv)) {
                  if (vv in sets) {
                    sets[vv] += 1;
                  } else {
                    sets[vv] = 1;
                  }
                }
              });
            } else {
              if (v in sets) {
                sets[v] += 1;
              } else {
                sets[v] = 1;
              }
            }
          }
        } else {
          let sets = pre["k2"] || {};
          if (value[this.key]) {
            let v = value[this.key];
            if (isArray(v) && v.length && v.length > 0) {
              v.forEach((vv) => {
                if (isString(vv)) {
                  if (vv in sets) {
                    sets[vv] += 1;
                  } else {
                    sets[vv] = 1;
                  }
                }
              });
            } else {
              if (v in sets) {
                sets[v] += 1;
              } else {
                sets[v] = 1;
              }
            }
          }
        }
        return pre;
      }, {});

      let reproject = [
        {
          x: "食用",
          count1: 80,
          count2: 40,
          count: 120,
        },
        {
          x: "未食用",
          count1: 20,
          count2: 60,
          count: 80,
        },
        {
          x: "合计",
          count1: 100,
          count2: 100,
          count: 200,
        },
      ];
      // let keys = [...Object.keys(data.k1), ...Object.keys(data.k2)];
      // let sets = {};
      // keys.forEach((k) => {
      //   if(k in sets){
      //     return
      //   }
      //   else{
      //     sets[k] = 1
      //   }
      //   reproject.push({
      //     x: k === "undefined" ? "未填写" : k,
      //     count1: data.k1[k] || 0,
      //     ratio1: data.k2[k] || 0,
      //     count2: valiad_count - sets[k],
      //     ratio2: ((valiad_count - sets[k]) / valiad_count) * 100,
      //     RR: (sets[k] === valiad_count
      //       ? Infinity
      //       : sets[k] / (valiad_count - sets[k])
      //     ).toFixed(2),
      //     RR_P: 0,
      //   });
      // });
      this.tableData = reproject;
      return reproject;
    },
    init() {
      this.reproject();
    },
  },

  mounted() {
    this.init();
  },
};
</script>

<style>
.el-table .warning-row {
  background: oldlace;
}
</style>